<%@page import="dao.ChapterSession"%>
<%@page import="dao.Examination"%>
<%@page import="beans.Examinationbean"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
	String chapterid = request.getParameter("chapterid");
	List<ChapterSession> sessionList = (List<ChapterSession>)session.getAttribute("chapterSession");
	List<Integer> errorList = null;
	List<Integer> doneItems = null;
	if(sessionList !=null){
	    for(int j=0;j<sessionList.size();j++){
		if(chapterid.equals(sessionList.get(j).getChapterid())){
		    errorList = (List<Integer>)session.getAttribute(sessionList.get(j).getErrorSessionName());
		    doneItems = (List<Integer>)session.getAttribute(sessionList.get(j).getIdSessionName());
		    break;
		}
	    }	    
	}
	
	Examinationbean examinationbean = new Examinationbean();
	examinationbean.getItems(chapterid, doneItems, errorList);
	List<Examination> items1 = examinationbean.getItems1();
	List<Examination> items2 = examinationbean.getItems2();
	List<Examination> items3 = examinationbean.getItems3();
	session.setAttribute("item1", items1);
	session.setAttribute("item2", items2);
	session.setAttribute("item3", items3);
	//String countString = items1.size()+"@"+items2.size()+"@"+items3.size();
%>
<html>
<head>
<base href="<%=basePath%>">

<title>测试</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<link rel="stylesheet" type="text/css"
	href="./clEditer/jquery.cleditor.css" />
<style type="text/css">
body {
	font: normal 11px auto "Trebuchet MS", Verdana, Arial, Helvetica,
		sans-serif;
	color: #4f6b72;
	text-align: center;
	background: #E6EAE9;
	width: 85%;
}

.space{
	border: 0;
	border-bottom: 1px solid #ccc;
	overflow-x: visible;
	width: 100%;
}

#container {
	margin-left: auto;
	margin-right: auto;
	width: 800px;
	height: 700px;
	background: #f8f8f8;
	display: block;
	vertical-align: middle;
}

.ele {
	margin-top: 15px;
	margin-botton: 15px;
	text-align: center;
	width: 100%;
	display: block;
	vertical-align: middle;
}
</style>
<script type="text/javascript" src="./clEditer/jquery.min.js"></script>
<script type="text/javascript" src="./clEditer/jquery.cleditor.min.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		var num = parseInt(document.getElementById("cCounts").value);
		for ( var i = 0; i < num; i++) {
			var str = "#cAnswer" + (i+1);
			$(str).cleditor();
		}
	});

	function dataCheck(){
		var counts = parseInt(document.getElementById("cou1").value);
		var allchoosed = true;
		for(var i=0;i<counts;i++){
			var str = "option"+(i+1);
			var obj = document.getElementsByName(str);
			var choosed = false;
			for(var j=0;j<4;j++){
				if(obj[j].checked){
					choosed = true;
				}
			}
			if(!choosed){
				allchoosed = false;
				alert("你有未选择的题目。");
				break;
			}
			
		}
		if(allchoosed){
			examinationForm.action = "./common/examinationResult.jsp";
		}	
		return allchoosed;
	}
	
	function disableCopyPaste(elm) {
		elm.oncontextmenu = function() {
			return false;
		}
		elm.onkeydown = interceptKeys;
	}

	function interceptKeys(evt) {
		var evt = evt || window.event, c = evt.keyCode, ctrlDown = evt.ctrlKey;

		if (ctrlDown && c === 67) { // c   
			return false;
		} else if (ctrlDown && c === 86) { // v   
			return false;
		} else if (ctrlDown && c === 88) { // x   
			return false;
		} else {
			return true;
		}
	}

	//disableCopyPaste(document);
	
</script>
</head>

<body>
	<div id="container">
		<h2>测试</h2>
		<form name="examinationForm" method="post" action="" onSubmit="return dataCheck()">
			<div class="ele">
				<label>一.选择题</label><br>
				<input type="hidden" name="cou1" id="cou1" value="<%=items1.size() %>">
				<c:choose>
					<c:when test="<%=items1.size() <1 %>">
						<c:out value="没有相应的题目"></c:out>
					</c:when>
					<c:otherwise>
						<%
						    int sequence = 1;
										for(Examination examination:items1){
										    StringBuilder builder = new StringBuilder(examination.getQuestion());
										    int examinationid = examination.getExaminationid();
						%>
						<c:choose>
							<c:when test="<%=builder.equals("img") %>">
								<img alt="" src=".<%=examination.getQuestionpath()%>"><br>								
								<input type="checkbox" name="option<%=sequence%>" value="a">A&nbsp;&nbsp;
								<input type="checkbox" name="option<%=sequence%>" value="b">B&nbsp;&nbsp;
								<input type="checkbox" name="option<%=sequence%>" value="c">C&nbsp;&nbsp;
								<input type="checkbox" name="option<%=sequence%>" value="d">D
							</c:when>
							<c:otherwise>
								<%
								    int point = builder.indexOf("@@@");
												    String stem = builder.substring(0, point);
												    int i = 0;
												    point = point + 3;
												    int point2 = builder.indexOf("##",point);
												    ArrayList<String> optionList = new ArrayList<String>(); 
												    while(i < 4){
														optionList.add(builder.substring(point,point2));
														i++;
														point = point2 + 2;
														point2 = builder.indexOf("##", point);
														if(point2 == -1){
														    point2 = builder.length();
														}
												    }
								%>
								<span><%=sequence%>.<%=stem%>( )</span>
								<br>							
								<span><input type="checkbox" name="option<%=sequence%>"
									value="a">A.<%=optionList.get(0)%></span>
								<br>
								<span><input type="checkbox" name="option<%=sequence%>"
									value="b">B.<%=optionList.get(1)%></span>
								<br>
								<span><input type="checkbox" name="option<%=sequence%>"
									value="c">C.<%=optionList.get(2)%></span>
								<br>
								<span><input type="checkbox" name="option<%=sequence%>"
									value="d">D.<%=optionList.get(3)%></span>
								<br>
							</c:otherwise>
						</c:choose>
						
						<input type="hidden" name="aid<%=sequence%>"
							id="aid<%=sequence%>" value="<%=examinationid%>">
						<%
						    sequence ++;
										}
						%>
					</c:otherwise>
				</c:choose>
			</div>
			<div class="ele">
				<label>二.填空题</label><br>
				<c:choose>
					<c:when test="<%=items2.size() <1 %>">
						<c:out value="没有相应的题目"></c:out>
					</c:when>
					<c:otherwise>
						<%
						    int sequence = 1;
										int spaceNumber = 1;
										for(Examination examination:items2){
										    int examinationid = examination.getExaminationid();	
										    String question = examination.getQuestion();
						%>
						<c:choose>
							<c:when test="<%=question.equals("img") %>">
								<img alt="" src=".<%=examination.getQuestionpath()%>">
							</c:when>
							<c:otherwise>
								<%
								    question.replaceAll("&nbsp", "_______");
								%>	
								<span><%=sequence%>.<%=question%></span>							
							</c:otherwise>
						</c:choose>
						<input type="hidden" name="bid<%=sequence%>"
									id="bid<%=sequence%>" value="<%=examinationid%>">								
						<span>答案：(多个空之前用";"分开)</span>
						<input type="text" class="space" name="bAnswer<%=sequence%>"
							id="bAnswer<%=sequence%>">
						<%
						    sequence ++;
										}
						%>
						<input type="hidden" name="spaceCounts" id="spaceCounts"
							value="<%=spaceNumber-1%>">
					</c:otherwise>
				</c:choose>
			</div>
			<div class="ele">
				<label>三.解答题</label><br>
				<input type="hidden" name="spaceCounts" id="cCounts"
							value="<%=items3.size()%>">
				<c:choose>
					<c:when test="<%=items3.size()<1 %>">
						<c:out value="没有相应的题目"></c:out>
					</c:when>
					<c:otherwise>
						
						<%
						    int sequence = 1;
										for(Examination examination:items3){
										    String question = examination.getQuestion();
										    int examinationid = examination.getExaminationid();
						%>
						<c:choose>
							<c:when test="<%=question.equals("img") %>">
								<img alt="" src=".<%=examination.getQuestionpath()%>">
							</c:when>
							<c:otherwise>								    
				   			<span><%=sequence%>.<%=question%></span>
								<br>
							</c:otherwise>
						</c:choose>
						<input type="hidden" name="cid<%=sequence %>" id="cid<%=sequence%>" value="<%=examinationid%>">
						<span>答案：</span>
						<br>
						<textarea name="cAnswer<%=sequence%>" id="cAnswer<%=sequence%>"></textarea>
						<br>
						<%
						    sequence ++;
										}
						%>
					</c:otherwise>
				</c:choose>
			</div>
			<div class="ele">
				<input type="submit" value="提交">&nbsp;&nbsp;&nbsp;&nbsp; 
				<input type="reset" value="取消">
			</div>
		</form>
	</div>
</body>
</html>
